package qiniu

import (
	"context"
	"fmt"
	"github.com/qiniu/go-sdk/v7/auth/qbox"
	"github.com/qiniu/go-sdk/v7/storage"
	"oss-uploader/lib"
)

var (
	token        string
	formUploader *storage.FormUploader
)

func Conn() {
	mac := qbox.NewMac(lib.QiniuCfg.AccessKey, lib.QiniuCfg.SecretKey)

	putPolicy := storage.PutPolicy{
		Scope:      lib.QiniuCfg.Bucket,
		ReturnBody: `{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}`,
	}
	token = putPolicy.UploadToken(mac)

	config := storage.Config{}
	config.Zone = &storage.ZoneHuanan // 空间对应的机房

	formUploader = storage.NewFormUploader(&config)
}

func UploadFile(key, localFile string) {
	ret := lib.QiniuPutRet{}
	err := formUploader.PutFile(context.Background(), &ret, token, key, localFile, nil)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(ret)
}
